package com.edu.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.dao.TcheckDao;
import com.edu.entity.Tcheck;
import com.edu.service.TcheckService;
import com.edu.service.Impl.TcheckServiceImpl;


/**
 * Servlet implementation class DriverServlet
 */
@WebServlet("/TcheckServlet")
public class TcheckServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String menu = request.getParameter("menu");
		System.out.println(menu);
		//查询
		if ("1".equals(menu)) {
			queryByTcheck(request,response);
		//修改
		}else if("2".equals(menu)) {
			TcheckUpdate(request, response);
		//添加
		}else if("3".equals(menu)) {
			TcheckAdd(request, response);
		//删除
		}else if("4".equals(menu)) {
			TcheckDelete(request, response);
		}else {
			System.out.println("sorry!");
		}
	}

	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void queryByTcheck(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int tcheckId = -1;
		if(!("".equals(request.getParameter("tcheckId")))){
			tcheckId = Integer.parseInt(request.getParameter("tcheckId"));
		}
		String tCompany = request.getParameter("tCompany");
		String tType = request.getParameter("tType");
		String tTime = request.getParameter("tTime");
		String dJob = request.getParameter("dJob");
		String dName = request.getParameter("dName");
		String tAdress = request.getParameter("tAdress");
		
		Tcheck tcheck = new Tcheck(tcheckId,tCompany,tType,tTime,dJob,dName,tAdress);
		TcheckService tcheckService = new TcheckServiceImpl();
		List<Tcheck> list = tcheckService.selectByTcheck(tcheck);
		request.setAttribute("list",list);
		request.getRequestDispatcher("Tcheck.jsp").forward(request, response);
	}
	
	protected void TcheckUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int tcheckId = Integer.parseInt(request.getParameter("tcheckId"));
		String tCompany = request.getParameter("tCompany");
		String tType = request.getParameter("tType");
		String tTime = request.getParameter("tTime");
		String dJob = request.getParameter("dJob");
        String dName = request.getParameter("dName");
		String tAdress = request.getParameter("tAdress");
		
		Tcheck tcheck = new Tcheck(tcheckId,tCompany,tType,tTime,dJob,dName,tAdress);
		TcheckService tcheckService = new TcheckServiceImpl();
		
		tcheckService.updateTcheck(tcheck);
		response.sendRedirect("Tcheck.jsp");
	}
	protected void TcheckAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String tCompany = request.getParameter("tCompany");
		String tType = request.getParameter("tType");
		String tTime = request.getParameter("tTime");
		String dJob = request.getParameter("dJob");
        String dName = request.getParameter("dName");
		String tAdress = request.getParameter("tAdress");
		
		Tcheck tcheck = new Tcheck(tCompany,tType,tTime,dJob,dName,tAdress);
		TcheckService tcheckService = new TcheckServiceImpl();
		
		tcheckService.addTcheck(tcheck);
		response.sendRedirect("Tcheck.jsp");
	}
	protected void TcheckDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String[] ids = request.getParameterValues("selecttcheckId");
		TcheckService tcheckService = new TcheckServiceImpl();
		
		tcheckService.deleteTcheckAll(ids);
		response.sendRedirect("Tcheck.jsp");	
	}
}
